import math
import os
import re
import struct
import threading
import pandas as pd
import numpy as np
import time
from commen.log import Logger
import openpyxl
import datetime
import commen.config


def ImuDatacontinuity():
    # ---------读取config文件获取筛选字段内容--------------------------
    mpukey = commen.config.global_config.getRaw('keywords', 'suanfagnss')
    caninskey = commen.config.global_config.getRaw('keywords', 'cankeygnss')

    # ---------读取config文件获取筛选字段内容--------------------------
    logger = Logger(logger='TestMylog').getlog()
    # -----------------------创建excel--------------------------------------------
    # 获取当前时间
    time_now = time.strftime("%Y%m%d-%H%M", time.localtime())
    # 获取当前路径
    result_root = os.getcwd()
    root = os.getcwd()
    result_path = result_root + r'\result' + "\\" + time_now
    log_path = root + r'\original-logfile' + '\\'
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    for root, dirs, files in os.walk(log_path):
        for file in files:
            if "Data" in file:
                aa = os.path.join(root, file)
            if "can" in file:
                bb = os.path.join(root, file)

    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    book = openpyxl.Workbook()  # 打开一个将写的文件
    sheet = book.create_sheet(index=0)  # 在将写的文件创建sheet
    saveExcel = result_path + "GNSS数据准确性验证.xlsx"
    sheet.cell(1, 2).value = "mpu上GNSS数据的系统时间"
    sheet.cell(1, 3).value = "can上GNSS数据系统时间"
    sheet.cell(1, 4).value = "mpu上GNSS数据内容"
    sheet.cell(1, 5).value = "can上GNSS数据内容"
    sheet.cell(1, 6).value = "对比数据信息"
    sheet.cell(1, 7).value = "数据是否一致"

    row = 1
    # -----------------------创建excel--------------------------------------------

    canfile = open(bb, 'r', encoding='gbk')  # 打开can.log目标文件
    dataprocfile = open(aa, 'r', encoding='UTF-8')  # 打开dataproc.log文件
    logger.info("打开目标文件")
    canlines = canfile.readlines()
    mpulines = dataprocfile.readlines()
    logger.info("获取内容")

    mpusystemtimelist = []  # mpu数据的系统时间
    cansystemtimelist = []  # can数据的系统时间
    mpumessagelist = []  # mpu数据内容
    canmessagelist = []  # can数据内容
    mpu_utctimelist = []# mpuutc时间
    can_utctimelist = []# canutc时间
    logger.info("开始逐行读取")

    for lines in mpulines:  # 对内容 进行逐行读取


        if mpukey in lines:  # 通过关键字获取数据信息并存入到list中

            mpusystemtime = lines[:lines.index("<thread")]
            mpusystemtimelist.append(mpusystemtime.rstrip())
            mpumessage = lines.split("+0:")
            mpumessage1 = mpumessage[1].lstrip().replace(" ","").replace("-","").replace("+10:","").replace("+20:","").replace("+30:","").replace("+40:","").replace("+50:","").replace("+60:","").replace("+70:","").replace("+80:","").replace(")","")
            mpumessagelist.append(mpumessage1.rstrip())
            mpu_utctimelist.append(mpumessage1.rstrip()[5:16])

            #utc时间：str(mpumessage1.rstrip()[5:7])+" "+str(mpumessage1.rstrip()[7:9])+" "+str(mpumessage1.rstrip()[9:11])+" "+str(mpumessage1.rstrip()[11:13])+" "+str(mpumessage1.rstrip()[13:15])
    for canline in canlines:   # 对内容 进行逐行读取
        if caninskey in canline:# 通过关键字获取数据信息并存入到list中
            systemtime = canline[:canline.index("Rx")]
            cansystemtimelist.append(systemtime.rstrip())
            canmessage = canline.split("s-fd-brs 64")
            canmessagelist.append(canmessage[1].replace(" ",""))
            can_utctimelist.append(canmessage[1].replace(" ","")[4:15])


    for i in mpu_utctimelist:

        if i in can_utctimelist:
            logger.info("开始进行对比")
            row = row+1
            mpu_number = mpu_utctimelist.index(str(i))
            can_number = can_utctimelist.index(str(i))
# -----------------------------------------------解算卫星数---------------------------------------------
            mpu_GNSSinf1PVTnumSV1 = mpumessagelist[mpu_number][18:20]
            mpu_GNSSinf1PVTnumSV2 = int(mpu_GNSSinf1PVTnumSV1, 16)
            mpu_GNSSinf1PVTnumSV3 = mpu_GNSSinf1PVTnumSV2 << 2
            mpu_GNSSinf1PVTnumSV_Decimal = mpu_GNSSinf1PVTnumSV3
            mpu_GNSSinf1PVTnumSV_Hexadecimal = str(hex(int(mpu_GNSSinf1PVTnumSV_Decimal))).replace("0x","")
            can_GNSSinf1PVTnumSV_Hexadecimal = canmessagelist[can_number][50:52]
            can_GNSSinf1PVTnumSV_Decimal = int(str(canmessagelist[can_number][50:52]),16)

# -----------------------------------------------周内秒-周---------------------------------------------
            mpu_GNSSinf1PVTweek = mpumessagelist[mpu_number][20:24]
            mpu_GNSSinf1PVTweek1 = int(mpu_GNSSinf1PVTweek, 16)
            mpu_GNSSinf1PVTweek2 = mpu_GNSSinf1PVTweek1 << 4
            mpu_GNSSinf1PVTweek_Decimal = mpu_GNSSinf1PVTweek2
            mpu_GNSSinf1PVTweek_Hexadecimal = str(hex(int(mpu_GNSSinf1PVTweek_Decimal))).replace("0x","")
            can_GNSSinf1PVTweek_Hexadecimal = canmessagelist[can_number][90:94]
            can_GNSSinf1PVTweek_Decimal = int(str(canmessagelist[can_number][90:94]),16)

# -----------------------------------------------周内秒-秒---------------------------------------------
            mpu_GNSSinf1PVTiTOW = mpumessagelist[mpu_number][24:32]
            mpu_GNSSinf1PVTiTOW1 = int(mpu_GNSSinf1PVTiTOW, 16)
            mpu_GNSSinf1PVTiTOW_Decimal = mpu_GNSSinf1PVTiTOW1
            mpu_GNSSinf1PVTiTOW_Hexadecimal = str(hex(int(mpu_GNSSinf1PVTiTOW1))).replace("0x", "")
            can_GNSSinf1PVTiTOW_Hexadecimal = canmessagelist[can_number][94:102]
            can_GNSSinf1PVTiTOW_Decimal = int(str(canmessagelist[can_number][94:102]), 16)

#-----------------------------------------------RTK经度---------------------------------------------
            mpu_RTK_Longitude_numbei =mpumessagelist[mpu_number][32:48]
            mpu_RTK_Longitude_numbei1 = int(mpu_RTK_Longitude_numbei,16)
            mpu_RTK_Longitude_numbei2 = int(mpu_RTK_Longitude_numbei1 / 13.097)
            mpu_RTK_Longitude_Decimal = mpu_RTK_Longitude_numbei2 << 2
            mpu_RTK_Longitude_Hexadecimal = hex(mpu_RTK_Longitude_Decimal)[2:].upper()
            can_RTK_Longitude_Hexadecimal = canmessagelist[can_number][40:50]
            can_RTK_Longitude_Decimal = int (str(can_RTK_Longitude_Hexadecimal),16)
# -----------------------------------------------RTK纬度---------------------------------------------
            mpu_RTK_Latitude_numbei = mpumessagelist[mpu_number][48:64]
            mpu_RTK_Latitude_numbei1 = int(mpu_RTK_Latitude_numbei, 16)
            mpu_RTK_Latitude_numbei2 = int(mpu_RTK_Latitude_numbei1 / 13.097)
            mpu_RTK_Latitude_Decimal = mpu_RTK_Latitude_numbei2 << 3
            mpu_RTK_Latitude_Hexadecimal= hex(mpu_RTK_Latitude_Decimal)[2:].upper()
            can_RTK_Latitude_Hexadecimal = canmessagelist[can_number][30:40]
            can_RTK_Latitude_Decimal = int(str(can_RTK_Latitude_Hexadecimal),16)
# -----------------------------------------------RTK高程---------------------------------------------
            mpu_RTK_Height_numbei = mpumessagelist[mpu_number][64:80]
            mpu_RTK_Height_numbei1 = int(mpu_RTK_Height_numbei, 16)
            mpu_RTK_Height_numbei2 = mpu_RTK_Height_numbei1+1000000000
            mpu_RTK_Height_numbei3 = int(mpu_RTK_Height_numbei2 / 2384.2)
            mpu_RTK_Height_Decimal = mpu_RTK_Height_numbei3 << 2
            mpu_RTK_Height_Hexadecimal = hex(mpu_RTK_Height_Decimal)[2:].upper()
            can_RTK_Height_Hexadecimal = canmessagelist[can_number][24:30]
            can_RTK_Height_Decimal = int(str(can_RTK_Height_Hexadecimal),16)
 # -----------------------------------------------东向速度---------------------------------------------
            mpu_East_vehicle_speed = mpumessagelist[mpu_number][80:88]
            mpu_East_vehicle_speed1 = struct.unpack("I", (int(str(mpu_East_vehicle_speed), 16)).to_bytes(4, byteorder='big'))
            mpu_East_vehicle_speed2 = struct.unpack("i", (mpu_East_vehicle_speed1[0]).to_bytes(4, byteorder='big'))
            mpu_East_vehicle_speed3= int(mpu_East_vehicle_speed2[0] / 1525.9)
            mpu_East_vehicle_Decimal =int(str(mpu_East_vehicle_speed3 << 7).replace("-",""))
            mpu_East_vehicle_Hexadecimal = hex(int(str(mpu_East_vehicle_Decimal).replace("-","")))[2:].upper()
            can_East_vehicle_Hexadecimal = canmessagelist[can_number][78:84]
            can_East_vehicle_Decimal = int(str(can_East_vehicle_Hexadecimal),16)
# -----------------------------------------------北向速度---------------------------------------------
            mpu_North_vehicle_speed = mpumessagelist[mpu_number][88:96]
            mpu_North_vehicle_speed1 = struct.unpack("I",(int(str(mpu_North_vehicle_speed), 16)).to_bytes(4, byteorder='big'))
            mpu_North_vehicle_speed2 = struct.unpack("i", (mpu_North_vehicle_speed1[0]).to_bytes(4, byteorder='big'))
            mpu_North_vehicle_speed3 = int(mpu_North_vehicle_speed2[0] / 1525.9)
            mpu_North_vehicle_Decimal = int(str(mpu_North_vehicle_speed3 << 7).replace("-",""))
            mpu_North_vehicle_Hexadecimal = hex(int(str(mpu_North_vehicle_Decimal).replace("-","")))[2:].upper()
            can_North_vehicle_speed_Hexadecimal = canmessagelist[can_number][84:90]
            can_North_vehicle_speed_Decimal  = int(str(can_North_vehicle_speed_Hexadecimal),16)
# -----------------------------------------------地向速度---------------------------------------------
            mpu_Downward_vehicle_speed = mpumessagelist[mpu_number][96:104]
            mpu_Downward_vehicle_speed1 = struct.unpack("I", (int(str(mpu_Downward_vehicle_speed), 16)).to_bytes(4,byteorder='big'))
            mpu_Downward_vehicle_speed2 = struct.unpack("i", (mpu_Downward_vehicle_speed1[0]).to_bytes(4, byteorder='big'))
            mpu_Downward_vehicle_speed3 = int(mpu_Downward_vehicle_speed2[0] / 1831.1)
            mpu_Downward_vehicle_Decimal = int(str(mpu_Downward_vehicle_speed3 << 1).replace("-",""))
            mpu_Downward_vehicle_speed_Hexadecimal = hex(int(str(mpu_Downward_vehicle_Decimal).replace("-","")))[2:].upper()
            can_Downward_vehicle_speed_Hexadecimal = canmessagelist[can_number][74:78]
            can_Downward_vehicle_speed_Decimal = int(str(can_Downward_vehicle_speed_Hexadecimal),16)
# -----------------------------------------------航向角---------------------------------------------
            mpu_Track_angle = mpumessagelist[mpu_number][104:112]
            mpu_Track_angle1 = int(str(mpu_Track_angle), 16)
            mpu_Track_angle2 = int(mpu_Track_angle1 / 10986)
            mpu_Track_angle_Decimal = mpu_Track_angle2 << 1
            mpu_Track_angle_Hexadecimal = hex(mpu_Track_angle_Decimal)[2:].upper()
            can_Track_angle_Hexadecimal = canmessagelist[can_number][16:20]
            can_Track_angle_Decimal = int(str(can_Track_angle_Hexadecimal),16)
# -----------------------------------------------RTK经度精度---------------------------------------------
            mpu_Longitude_STD = mpumessagelist[mpu_number][112:120]
            mpu_Longitude_STD1 = int(str(mpu_Longitude_STD), 16)
            mpu_Longitude_STD2 = int(mpu_Longitude_STD1 / 92773)
            mpu_Longitude_STD_Decimal = mpu_Longitude_STD2 << 4
            mpu_Longitude_STD_Hexadecimal = hex(mpu_Longitude_STD_Decimal)[2:].upper()
            can_Longitude_STD_Hexadecimal = canmessagelist[can_number][66:70]
            can_Longitude_STD_Decimal = int(str(can_Longitude_STD_Hexadecimal),16)
# -----------------------------------------------RTK纬度精度---------------------------------------------
            mpu_Latitude_STD = mpumessagelist[mpu_number][120:128]
            mpu_Latitude_STD1 = int(str(mpu_Latitude_STD), 16)
            mpu_Latitude_STD2 = int(mpu_Latitude_STD1 / 92773)
            mpu_Latitude_STD_Decimal = mpu_Latitude_STD2 << 4
            mpu_Latitude_STD_Hexadecimal = hex(mpu_Latitude_STD_Decimal)[2:].upper()
            can_Latitude_STD_Hexadecimal= canmessagelist[can_number][62:66]
            can_Latitude_STD_Decimal  = int(str(can_Latitude_STD_Hexadecimal),16)
# -----------------------------------------------RTK高程精度---------------------------------------------
            mpu_Height_STD = mpumessagelist[mpu_number][128:136]
            mpu_Height_STD1 = int(str(mpu_Height_STD), 16)
            mpu_Height_STD2 = int(mpu_Height_STD1 / 92773)
            mpu_Height_STD_Decimal = mpu_Height_STD2 << 4
            mpu_Height_STD_Hexadecimal = hex(mpu_Height_STD_Decimal)[2:].upper()
            can_Height_STD_Hexadecimal = canmessagelist[can_number][58:62]
            can_Height_STD_Decimal = int(str(can_Height_STD_Hexadecimal),16)
#-----------------------------------------------水平分量精度因子---------------------------------------------
            mpu_Horizon_DOP = mpumessagelist[mpu_number][136:144]
            mpu_Horizon_DOP1 = int(str(mpu_Horizon_DOP), 16)
            mpu_Horizon_DOP2 = int(mpu_Horizon_DOP1 / 92773)
            mpu_Horizon_DOP_Decimal = mpu_Horizon_DOP2 << 7
            mpu_Horizon_DOP_Hexadecimal = hex(mpu_Horizon_DOP_Decimal)[2:].upper()
            can_Horizon_DOP_Hexadecimal = canmessagelist[can_number][20:24]
            can_Horizon_DOP_Decimal = int(str(can_Horizon_DOP_Hexadecimal),16)
# -----------------------------------------------位置精度因子---------------------------------------------
            mpu_Position_DOP = mpumessagelist[mpu_number][144:152]
            mpu_Position_DOP1 = int(str(mpu_Position_DOP), 16)
            mpu_Position_DOP2 = int(mpu_Position_DOP1 / 92773)
            mpu_Position_DOP_Decimal = mpu_Position_DOP2 << 7
            mpu_Position_DOP_Hexadecimal = hex(mpu_Position_DOP_Decimal)[2:].upper()
            can_Position_DOP_Hexadecimal = canmessagelist[can_number][52:56]
            can_Position_DOP_Decimal = int(str(can_Position_DOP_Hexadecimal),16)

#-----------------------------------------------垂直分量精度因子---------------------------------------------
            mpu_Vertical_DOP = mpumessagelist[mpu_number][152:160]
            mpu_Vertical_DOP1 = int(str(mpu_Vertical_DOP), 16)
            mpu_Vertical_DOP2 = int(mpu_Vertical_DOP1 / 92773)
            mpu_Vertical_DOP_Decimal = mpu_Vertical_DOP2 << 7
            mpu_Vertical_DOP_Hexadecimal = hex(mpu_Vertical_DOP_Decimal)[2:].upper()
            can_Vertical_DOP_Hexadecimal = canmessagelist[can_number][70:74]
            can_Vertical_DOP_Decimal = int(str(can_Vertical_DOP_Hexadecimal),16)

# -----------------------------------------------域控时间---------------------------------------------
            mpu_Timestamp_to_can = mpumessagelist[mpu_number][160:172]
            can_Timestamp = canmessagelist[can_number][102:114]


            if  mpu_RTK_Longitude_Decimal == can_RTK_Longitude_Decimal  and mpu_RTK_Latitude_Decimal == can_RTK_Latitude_Decimal :
                sheet.cell(row, 2).value = mpusystemtimelist[mpu_number]
                sheet.cell(row, 3).value = cansystemtimelist[can_number]
                sheet.cell(row, 4).value = mpumessagelist[mpu_number]
                sheet.cell(row, 5).value = canmessagelist[can_number]
                sheet.cell(row, 6).value =  str("mpu_RTK_Longitude_Decimal:")+ str(mpu_RTK_Longitude_Decimal) +'\r\n'+ str("can_RTK_Longitude_Decimal:")+  str(can_RTK_Longitude_Decimal)  +'\r\n'+ str("mpu_RTK_Latitude_Decimal:")+  str(mpu_RTK_Latitude_Decimal) +'\r\n'+ str("can_RTK_Latitude_Decimal:")+ str(can_RTK_Latitude_Decimal)
                sheet.cell(row, 7).value = "是"
            else:
                sheet.cell(row, 2).value = mpusystemtimelist[mpu_number]
                sheet.cell(row, 3).value = cansystemtimelist[can_number]
                sheet.cell(row, 4).value = mpumessagelist[mpu_number]
                sheet.cell(row, 5).value = canmessagelist[can_number]
                sheet.cell(row, 6).value = str("mpu_RTK_Longitude_Decimal:")+ str(mpu_RTK_Longitude_Decimal) +'\r\n'+ str("can_RTK_Longitude_Decimal:")+  str(can_RTK_Longitude_Decimal)  +'\r\n'+ str("mpu_RTK_Latitude_Decimal:")+  str(mpu_RTK_Latitude_Decimal) +'\r\n'+ str("can_RTK_Latitude_Decimal:")+ str(can_RTK_Latitude_Decimal)
                sheet.cell(row, 7).value = "否"


    # ----------------------将对比的数据写入excel---------------------------------
    book.save(saveExcel)
    # ----------------------将对比的数据写入excel---------------------------------

ImuDatacontinuity()




